<?php
include './header.php';
//die;
$condition = "LIMIT {$offset},{$limit}";
$count = $db->selectOne('case_type', 'COUNT(case_type_id)')[0];
$cases = $db->selectAll('case_type', '*', $condition);


//修改
if (isset($_GET['edit_id']) && $_GET['edit_id'] > 0) {
    $id = $_GET['edit_id'];
    $edit_cond = "WHERE case_type_id = {$id}";
    $one = $db->selectOne('case_type', '*', $edit_cond);

    if (!empty($_POST['edit'])) {
        $update_condition = "WHERE case_type_id = $id";
        $value = '';
        if ($_FILES['pic1']['error'] == 0) {
            $old_img = $_POST['old_img'];
            $old_thumb = $_POST['old_thumb'];
            $case_img_mark = $_POST['old_img_mark'];
            $case_str_mark = $_POST['old_str_mark'];

            if (is_file($old_img)) {
                unlink($old_img);
            }
            if (is_file($old_thumb)) {
                unlink($old_thumb);
            }
            if (is_file($case_img_mark)) {
                unlink($case_img_mark);
            }
            if (is_file($case_str_mark)) {
                unlink($case_str_mark);
            }

            $pic_conf = [
                'table' => 'case_type',
                'name' => 'pic1',
                'prefix' => 'nnd_'
            ];
            $up = Upload::getInstance($pic_conf);
            $up_img = $up->downLoad();
            if ($up_img['code'] == 1) {
                $info_img = $up_img['path'];
                $dir = $up_img['dir'];
                $name = $up_img['new_name'];
                //缩略图参数
                $thumb_conf = [
                    'img_addr' => $info_img,
                    'path' => $dir,
                    'name' => $name,
                    'width' => 150,
                    'height' => 102
                ];
                $th = Thumb::getInstance($thumb_conf);
                $thumb = $th->download();

                // 文字水印参数
                $str_conf = [
                    'fonts' => 'fonts/STXINGKA.TTF',
                    'str' => '哈哈哈',
                    'img_addr' => $info_img,
                    'img_name' => $name,
                    'dir' => $dir
                ];
                $strmark = StrMark::getInstance($str_conf);
                $str_mark = $strmark->make();

                // 图片水印参数
                $img_conf = [
                    'ori_img' => $info_img,
                    'water_img' => 'upload/logo.png',
                    'name' => $name,
                    'dir' => $dir
                ];
                $imgmark = ImgMark::getInstance($img_conf);
                $img_mark = $imgmark->make();
                
                $value .= '`case_type_img`="' . $info_img . '",';
                $value .= '`case_type_thumb`="' . $thumb . '",';
                $value .= '`case_type_strmark`="' . $str_mark . '",';
                $value .= '`case_type_imgmark`="' . $img_mark . '",';
            }
        }

        foreach ($_POST as $key => $val) {
            if ($val != '' && $val != '提交' && $key != 'old_img' && $key != 'old_thumb' && $key != 'old_img_mark' && $key != 'old_str_mark') {
                $value .= '`' . $key . '`' . "='" . $val . "',";
            }
        }

        $time = time();
        $value = $value . "`case_type_time` = '$time'";
        //         pre($value);die;
        if ($db->update('case_type', $value, $update_condition)) {
            echo "<script>alert('修改成功！');window.location.href='case_type.php'</script>";
        } else {
            echo "<script>alert('修改失败！')</script>";
        }
    }
}


?>

<link href="css/bootstrap.css" rel="stylesheet">
<link href="css/bootstrap-fileinput.css" rel="stylesheet">
<!-- Start: Content -->
<section id="content">
    <div id="topbar" class="affix">
        <ol class="breadcrumb">
            <li><a href="#"><span class="glyphicon glyphicon-home"></span></a></li>
            <li class="if($ca_type == ''){echo 'active';}">案例分类管理</li>
            <?php if ($ca_type != '') { ?>
                <li class="active"><?php echo $cases[0]['case_type_name1']; ?></li>
            <?php } ?>
        </ol>
    </div>
    <div class="container">

        <div class="row">
            <div class="col-md-12">
                <div class="panel">
                    <form action="" method="post" class="cmxform" enctype="multipart/form-data">
                        <div class="panel">
                            <div class="panel-heading">
                                <div class="panel-title">编辑案例分类</div>
                                <div class="panel-btns pull-right margin-left">
                                    <a href="#" onclick="window.history.back();" class="btn btn-default btn-gradient dropdown-toggle"><span class="glyphicon glyphicon-chevron-left"></span></a>
                                </div>
                            </div>
                            <div class="panel-body">
                                <div class="col-md-7">

                                    <div class="form-group">
                                        <div class="input-group"><span class="input-group-addon">标题</span>
                                            <input type="text" name="case_type_name1" value="<?php echo $one['case_type_name1']; ?>" class="form-control">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <div class="input-group"><span class="input-group-addon">英文标题</span>
                                            <input type="text" name="case_type_name2" value="<?php echo $one['case_type_name2']; ?>" class="form-control">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <input type="hidden" name="old_img_mark" value="<?php echo $one['case_type_imgmark']; ?>">
                                        <input type="hidden" name="old_str_mark" value="<?php echo $one['case_type_strmark']; ?>">
                                        <input type="hidden" name="old_img" value="<?php echo $one['case_type_img']; ?>">
                                        <input type="hidden" name="old_thumb" value="<?php echo $one['case_type_thumb']; ?>">
                                        <div class="fileinput fileinput-new" data-provides="fileinput" id="exampleInputUpload">
                                            <div class="fileinput-new thumbnail" style="width: 200px;height: auto;max-height:150px;">
                                                <img id='picImg' style="width: 100%;height: auto;max-height: 140px;" src="<?php echo ROOT . '/Admin/' . $one['case_type_thumb']; ?>" alt="" />
                                            </div>
                                            <div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 150px;"></div>
                                            <div>
                                                <span class="btn btn-primary btn-file">
                                                    <span class="fileinput-new">更换图片</span>
                                                    <span class="fileinput-exists">换一张</span>
                                                    <input type="file" name="pic1" id="picID" accept="image/gif,image/jpeg,image/x-png" />
                                                </span>
                                                <a href="javascript:;" class="btn btn-warning fileinput-exists" data-dismiss="fileinput">移除</a>
                                            </div>
                                        </div>
                                    </div>

                                </div>

                                <div class="col-md-7">
                                    <div class="form-group">
                                        <input type="submit" value="提交" name="edit" class="submit btn btn-blue">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</section>
<!-- End: Content -->
</div>
<!-- End: Main -->
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap-fileinput.js"></script>
<script>
    $(function() {
        //比较简洁，细节可自行完善
        $('#uploadSubmit').click(function() {
            var data = new FormData($('#uploadForm')[0]);
            $.ajax({
                url: 'xxx/xxx',
                type: 'POST',
                data: data,
                async: false,
                cache: false,
                contentType: false,
                processData: false,
                success: function(data) {
                    console.log(data);
                    if (data.status) {
                        console.log('upload success');
                    } else {
                        console.log(data.message);
                    }
                },
                error: function(data) {
                    console.log(data.status);
                }
            });
        });

    })
</script>
</body>


</html>